﻿using System;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SeventhSenseSystems.HighFlyingPm.Business.Entities;
using SeventhSenseSystems.HighFlyingPm.Business.Models;
using WebBusiness;

namespace WebHost
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Wiki", // Route name
                "Wiki/{action}/{id}", // URL with parameters
                new
                    {
                        controller = "Wiki",
                        action = "Index",
                        id = UrlParameter.Optional,
                        project = "",
                        slug = "home"
                    }
                );
            routes.MapRoute(
                "ProjectWiki", // Route name
                "projects/{project}/wiki/{action}/{id}/{slug}", // URL with parameters
                new {controller = "Wiki", action = "Index", id = UrlParameter.Optional, slug = "home"}
                // Parameter defaults
                );

            routes.MapRoute(
                "ProjectIssue", // Route name
                "projects/{project}/issues/{action}/{id}", // URL with parameters
                new {controller = "Issue", action = "Index", id = UrlParameter.Optional}
                // Parameter defaults
                );
            
            routes.MapRoute(
                "Projects", // Route name
                "projects", // URL with parameters
                new {controller = "Home", action = "Projects"} // Parameter defaults
                );

            routes.MapRoute(
                "ProjectHome", // Route name
                "projects/{project}", // URL with parameters
                new {controller = "Projects", action = "Index"} // Parameter defaults
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
                );

            routes.MapRoute(
                "Admin", // Route name
                "admin/{controller}/{action}/{id}", // URL with parameters
                new {controller = "AdminHome", action = "Index", id = UrlParameter.Optional} // Parameter defaults
                );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            var catalog = new DirectoryCatalog(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));
            var container = new CompositionContainer(catalog);

            InitializeTestData(container);

            ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
        }

        private static void InitializeTestData(CompositionContainer container)
        {
            var trackerRepo = container.GetExportedValue<ITrackerRepository>();
            Guid bugId = Guid.NewGuid();
            trackerRepo.Add(new Tracker {Id = bugId, Name = "Bug"});
            trackerRepo.Add(new Tracker {Id = Guid.NewGuid(), Name = "Feature"});
            trackerRepo.Add(new Tracker {Id = Guid.NewGuid(), Name = "Support"});

            var projecRepo = container.GetExportedValue<IProjectRepository>();
            projecRepo.Add(new Project
                               {
                                   Id = Guid.NewGuid(),
                                   Name = "FirstProject",
                                   Description = "Description of first project",
                                   Identifier = "fp"
                               });
            projecRepo.Add(new Project
                               {
                                   Id = Guid.NewGuid(),
                                   Name = "SecondProject",
                                   Description = "Description of second project",
                                   Identifier = "sp"
                               });
            projecRepo.Add(new Project
                               {
                                   Id = Guid.NewGuid(),
                                   Name = "ThirdProject",
                                   Description = "Description of third project",
                                   Identifier = "tp"
                               });

            foreach (Project project in projecRepo.All)
            {
                project.Trackers.Add(trackerRepo.Find(bugId));
            }
        }
    }
}